package com.codeduck.validate.controller;

import com.codeduck.validate.entity.Book;
import org.hibernate.validator.constraints.Length;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;

/**
 * 1. 方法参数的校验 {@link ParamFieldValidateController#test1(String)}
 * 2. 对象属性的校验 {@link com.codeduck.validate.entity.Book}
 *
 * @author: <a href="mailto:codeduck@163.com">Codeduck</a>
 * @createDate: 2021-12-26
 */
@RestController
@Validated // 开启数据有效性校验，添加在类上即为验证方法，添加在方法参数中即为验证参数对象。（添加在方法上无效）
public class ParamFieldValidateController {

    @GetMapping("/index1")
    public String test1(
            @NotBlank(message = "name 不能为空")
            @Length(min = 2, max = 10, message = "name 长度必须在 {min} - {max} 之间") String name) {
        return "success";
    }

    @GetMapping("/index2")
    public String test2(@Validated Book book) {
        return "success";
    }


}
